
package processor;

import java.lang.reflect.Field;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

import com.e885.log.E885Logger;

/**
 * 當 Spring container 建立並設定完成後(在這裏是是 bean 的依賴關係), 在 bean 初始化之前, 會呼叫 postProcessBeforeInitialization, 在 bean 初始化之後,<br>
 * 會呼叫 postProcessAfterInitialization.<br>
 * 
 * 我們將在 bean 初始化之前, 將有關這個 bean 屬性的型別為字串, 回傳的值全部轉為大寫.<br>
 * 
 * @author Eric.Kao(gau168.gau@msa.hinet.net)
 * @version 1.0
 * @company E885 Group
 * @copyright Copyright (c) 2001-2008
 */

public class MyBeanPostProcessor implements BeanPostProcessor
{

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.beans.factory.config.BeanPostProcessor#postProcessAfterInitialization(java.lang.Object, java.lang.String)
	 */
	public Object postProcessAfterInitialization( Object bean, String beanName ) throws BeansException
	{
		E885Logger.springLog.info( "execute method postProcessAfterInitialization()" );

		return bean;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.beans.factory.config.BeanPostProcessor#postProcessBeforeInitialization(java.lang.Object, java.lang.String)
	 */
	public Object postProcessBeforeInitialization( Object bean, String beanName ) throws BeansException
	{
		E885Logger.springLog.info( "execute method postProcessBeforeInitialization()" );

		Field[] fields = bean.getClass().getDeclaredFields();

		int length = fields.length;

		for( int i = 0; i < length; i++ )
		{
			if( fields[ i ].getType().equals( String.class ) )
			{
				fields[ i ].setAccessible( true );

				try
				{
					fields[ i ].set( bean, ( ( String )fields[ i ].get( bean ) ).toUpperCase() );
				}
				catch( IllegalArgumentException e )
				{
					e.printStackTrace();
				}
				catch( IllegalAccessException e )
				{
					e.printStackTrace();
				}
			}
		}

		return bean;
	}

}
